/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.embed.infra.configuration;

import org.cshoo.tattoo.embed.infra.entity.User;
import org.cshoo.tattoo.embed.infra.utils.UserUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.lang.NonNull;

import java.util.Objects;
import java.util.Optional;

/**
 * @author 杨翼
 * @since 1.0
 *
 * spring-common-data 中的审计字段的用户字段值来自此类，spring约定
 *
 */
@Configuration
public class CurrentUserAuditAware implements AuditorAware<String> {

    @Override
    @NonNull
    public Optional<String> getCurrentAuditor() {
        User user = UserUtils.getUser();
        if (Objects.nonNull(user)) {
            return Optional.ofNullable(user.getUsername());
        }
        return Optional.empty();
    }

}
